package org.sanguine.study.hometasks.first;

class VectorValuesFinder {
	public final double[] vector;
	private int indexOfMax;
	private double maxValue;
	private double minValue;
	private int indexOfMin;
	private int indexOfNextMax = -1;
	
	public VectorValuesFinder(double[] vector) {
		this.vector = vector;
		findValues();
	}
	
	private void findValues() {
		double max = vector[0], min = vector[0];
		int indexMax = 0, indexMin = 0;
		
		for (int i = 1; i < vector.length; i++)
			if (max < vector[i]) {
				max = vector[i];
				indexMax = i;
			} else if (min > vector[i]) {
				min = vector[i];
				indexMin = i;
			}
		
		this.indexOfMax = indexMax;
		this.maxValue = max;
		this.indexOfMin = indexMin;
		this.minValue = min;
	}

	public int getIndexOfMax() {
		return indexOfMax;
	}

	public double getMaxValue() {
		return maxValue;
	}
	
	public int getIndexOfMin() {
		return indexOfMin;
	}

	public double getMinValue() {
		return minValue;
	}

	public int getNextIndexOfMax() {
		boolean hasNext = false;
		
		for (int i = (indexOfNextMax < 0) ? indexOfMax + 1 : indexOfNextMax + 1; i < vector.length; i++)
			if (vector[i] == maxValue) {
				indexOfNextMax = i;
				hasNext = true;
				break;
			}
		if (!hasNext) indexOfNextMax = -1;
		return indexOfNextMax;
	}
}